<?php
require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
core::getResource("Model.Event");

class EventController extends baseController
{
	private $eventId;
	private $event;

	public function __construct($eventId)
	{
		$this->eventId = (int)$eventId;
		if($this->eventExists())
		{
			$this->event = new Event($this->eventId);
		}
		else
		{
			$this->event = null;
		}
	}

	public function eventExists()
	{
		if(Event::exists($this->eventId))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function print_details()
	{
		echo $this->event->eventName;
		echo "<br/>";
		echo $this->event->startTime;
		echo "<br/>";
		echo $this->event->description;
	}

	public function getName()
	{
		return $this->event->eventName;
	}

	public function getDescription()
	{
		return $this->event->eventDescription;
	}

	public function getStartAndEndTimes()
	{
		return $this->event->startTime." to ".$this->event->endTime;
	}

	public function getStartAndEndDates()
	{
		return $this->event->startDate." to ".$this->event->endDate;
	}

	public function getLocation()
	{
		return ($this->event->locationDetails == "NULL" ? "none" : $this->event->locationDetails);
	}

	public function meetingLocation()
	{
		return ($this->event->meetingLocation == "NULL" ? "none" : $this->event->meetingLocation);
	}

	public function meetingTime()
	{
		return ($this->event->meetingTime == "NULL" ? "none" : $this->event->meetingTime);
	}
    
    public function carpoolTime()
	{
		return ($this->event->carpoolTime == "NULL" ? "none" : $this->event->carpoolTime);
	}
    public function carpoolLocation()
	{
		return ($this->event->carpoolLocation == "NULL" ? "none" : $this->event->carpoolLocation);
	}
    

	public function getActivities()
	{
		return $this->event->getActivities();
	}

	public static function getAllEvents($startDate)
	{
		return Event::getEventByDate($startDate);
	}

	public static function getAllEventsOn($startDate)
	{
		return Event::getAllEventsOn($startDate);
	}

	public static function canDelete($id)
	{
		$db = new dbConn();
		$res = $db->query("SELECT * FROM activity WHERE event=$id");
		if($db->num_rows($res) > 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	public static function delete($id)
	{
		$db = new dbConn();
		$data = $db->query("SELECT activityId FROM activity JOIN event ON activity.event = event.eventId WHERE activity.event=$id");
        while($row = $db->fetch_array($data))
        {
            ActivityController::delete($row['activityId']);
        }
        $db->query("DELETE FROM manages WHERE event=$id");
		$db->query("DELETE FROM event WHERE eventId=$id");
	}
}

?>